/**
 * 
 */
package cn.edu.cdtu.healthserver.dao.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import cn.edu.cdtu.healthserver.dao.ChooseLogDao;
import cn.edu.cdtu.healthserver.dao.HealthFileMentalDao;
import cn.edu.cdtu.healthserver.dao.TestLogDao;
import cn.edu.cdtu.healthserver.domain.Choose;
import cn.edu.cdtu.healthserver.domain.ChooseLog;
import cn.edu.cdtu.healthserver.domain.HealthFileMental;
import cn.edu.cdtu.healthserver.domain.Test;
import cn.edu.cdtu.healthserver.domain.TestLog;
import cn.edu.cdtu.healthserver.domain.UserInformation;
import cn.edu.cdtu.healthserver.utils.C3P0Util;

/**
 * @author 
 *
 */
public class HealthFileMentalDaoImplementation implements HealthFileMentalDao {

	QueryRunner qr = new QueryRunner(C3P0Util.getDataSource());

	TestLogDao testLogDao = new TestLogDaoImplementation();
	ChooseLogDao chooseLogDao = new ChooseLogDaoImplementation();

	/*
	 * 获取最近的心理测试testlog (non-Javadoc)
	 * 
	 * @see
	 * cn.edu.scu.healthserver.dao.HealthFileMentalDao#getMentalLastTestLog(
	 * java.util.List)
	 */
	@Override
	public TestLog getMentalLastTestLog(List<TestLog> testLogs) {
		// TODO Auto-generated method stub

		for (TestLog testLog : testLogs) {
			if (isMentalTest(testLog)) {
				return testLog;
			}
		}
		return null;
	}

	/*
	 * 获取最近测试testlog中choose (non-Javadoc)
	 * 
	 * @see
	 * cn.edu.scu.healthserver.dao.HealthFileMentalDao#getChoose(java.util.List)
	 */
	@Override
	public List<Choose> getChoose(List<ChooseLog> chooseLogs) {
		// TODO Auto-generated method stub

		List<Choose> chooses = new ArrayList<Choose>();

		for (ChooseLog chooseLog : chooseLogs) {
			String sql = "select * from choose where id="
					+ chooseLog.getChoose_id();
			try {
				Choose choose = qr.query(sql,
						new BeanListHandler<Choose>(Choose.class)).get(0);
				if (choose.getPoint() > 0) {
					chooses.add(choose);
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				throw new RuntimeException();
			}
		}
		return chooses;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * cn.edu.scu.healthserver.dao.HealthFileMentalDao#getHealthFileMentalAnswer
	 * (java.util.List)
	 */
	@Override
	public HealthFileMental getHealthFileMentalAnswer(List<Choose> chooses) {
		// TODO Auto-generated method stub
		int anxietyValue = 0;
		int hysterValue = 0;
		int schoizophreniaValue = 0;
		int disorderValue = 0;
		int depressionValue = 0;
		int nervousValue = 0;
		int confabulationValue = 0;

		for (Choose choose : chooses) {
			switch (choose.getQuestion_id() % 7) {
			case 0:
				confabulationValue += choose.getPoint();
				break;
			case 1:
				anxietyValue += choose.getPoint();
				break;
			case 2:
				hysterValue += choose.getPoint();
				break;
			case 3:
				schoizophreniaValue += choose.getPoint();
				break;
			case 4:
				disorderValue += choose.getPoint();
				break;
			case 5:
				depressionValue += choose.getPoint();
				break;
			case 6:
				nervousValue += choose.getPoint();
				break;
			default:
				break;
			}
		}
		
		HealthFileMental mental = new HealthFileMental(anxietyValue, hysterValue, schoizophreniaValue, 
				disorderValue, depressionValue, nervousValue, confabulationValue);
		
		return mental;
		

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * cn.edu.scu.healthserver.dao.HealthFileMentalDao#getHealthFileMental(cn
	 * .edu.scu.healthserver.domain.UserInformation)
	 */
	@Override
	public HealthFileMental getHealthFileMental(UserInformation user) {
		// TODO Auto-generated method stub

		List<TestLog> testLogs = testLogDao.query(user);
		
		TestLog lastTestLog = getMentalLastTestLog(testLogs);
		
		List<ChooseLog> chooseLogs = chooseLogDao.query(lastTestLog);
		
		List<Choose> chooses = getChoose(chooseLogs);
		
		HealthFileMental healthFileMental = getHealthFileMentalAnswer(chooses);
		
		return healthFileMental;
		
		
		
		
	}

	// 判断是哪种测试类型
	public Boolean isMentalTest(TestLog testLog) {

		String sql = "select * from test where id = " + testLog.getTest_id();

		try {
			if (qr.query(sql, new BeanListHandler<Test>(Test.class)).get(0)
					.getType_id() == 1) {
				return true;
			}

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			throw new RuntimeException();
		}
		return false;
	}

}
